/**
 * 
 */
package net.techwatch.jaxb2;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import net.techwatch.jaxb2.model.Article;
import net.techwatch.jaxb2.model.Category;
import net.techwatch.jaxb2.model.METADONNEES;
import net.techwatch.jaxb2.model.ObjectFactory;
import net.techwatch.jaxb2.model.Stock;

/**
 * @author wiv
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance("net.techwatch.jaxb2.model");
		
		Marshaller marshaller = context.createMarshaller();
		Unmarshaller unmarshaller = context.createUnmarshaller();

		ObjectFactory factory = new ObjectFactory();
		Stock stock = factory.createStock();
		List<Category> categories = stock.getCategories();
		Category category = factory.createCategory();
		category.setName("vegetables");
		List<Article> articles = category.getArticles();
		Article article = factory.createArticle();
		article.setName("brocoli");
		article.setPrice(3.45);
		article.setQuantity(25);
		articles.add(article);
		categories.add(category);
		
		File xmlFile = new File("xml_file.xml");
		
		marshaller.marshal(stock, xmlFile);
		
		Stock unmarshallStock = (Stock) unmarshaller.unmarshal(xmlFile);
		List<Category> unmarshalCategories = unmarshallStock.getCategories();
		assert unmarshalCategories.size() == 1;
		List<Article> unmarshallArticles = unmarshalCategories.get(0).getArticles();
		assert unmarshallArticles.size() == 1;
		Article unmarshallArticle = unmarshallArticles.get(0);
		assert unmarshallArticle.getName().equals(article.getName());
		assert unmarshallArticle.getPrice() == article.getPrice();
		assert unmarshallArticle.getQuantity() == article.getQuantity();
	}

}
